package com.wmx.service.impl;

import com.wmx.entity.RoleEntity;
import com.wmx.entity.UserEntity;
import com.wmx.mapper.RoleMapper;
import com.wmx.mapper.UserMapper;
import com.wmx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.CountDownLatch;

@Service
@Transactional(readOnly =true,rollbackFor = {Exception.class},timeout = 30)
public class IUserService implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RoleMapper roleMapper;


    public void insertUser1(UserEntity userEntity, RoleEntity roleEntity) throws Exception {
        this.insertUser(userEntity,roleEntity);
    }

    @Transactional(readOnly = false,rollbackFor = {Exception.class})
    @Override
    public void insertUser(UserEntity userEntity, RoleEntity roleEntity) throws Exception {
        userMapper.insert(userEntity);
       /* int a= 1/0;
        roleMapper.insert(roleEntity);*/
    }


    @Transactional(readOnly = true,rollbackFor = {Exception.class})
    @Async //
    public UserEntity queryByUserEntity(Long id, CountDownLatch countDownLatch){
        try {
           return userMapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            countDownLatch.countDown();
        }
        return null;
    };
}
